package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.user.impl.dao.BreedFactoryStableDao;
import com.jiantong.user.model.BreedFactoryStable;
import com.jiantong.user.service.BreedFactoryStableReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 24/7/17.
 *
 */
@Slf4j
@RpcProvider
@Service
public class BreedFactoryStableReadServiceImpl implements BreedFactoryStableReadService{

    private final BreedFactoryStableDao breedFactoryStableDao;

    @Autowired
    public BreedFactoryStableReadServiceImpl(BreedFactoryStableDao breedFactoryStableDao) {
        this.breedFactoryStableDao = breedFactoryStableDao;
    }


    @Override
    public Response<BreedFactoryStable> findById(Long id) {

        try {
            return Response.ok(breedFactoryStableDao.findById(id));
        }catch (Exception e){
            log.error("failed to find breed factory stable case = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find breed factory stable");
        }
    }

    @Override
    public Response<Paging<BreedFactoryStable>> paging(Integer pageNo, Integer pageSize, Map<String, Object> criteria) {

        try {
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(breedFactoryStableDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging breed factory stable case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging breed factory stable");
        }
    }

    /**
     * 查询养殖场的栏舍的list
     *
     * @param breedFactoryId 养殖场的id
     * @return List<BreedFactoryStable>
     */
    @Override
    public Response<List<BreedFactoryStable>> findStableList(Long breedFactoryId) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();
            criteria.put("breedFactoryId",breedFactoryId);
            criteria.put("status", 1);
            return Response.ok(breedFactoryStableDao.findStableList(criteria));
        }catch (Exception e){
            log.error("failed to find stable list , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find stable list");
        }
    }

    /**
     * 栏舍是否存在
     *
     * @param breedFactoryId 养殖场的id
     * @return Long
     */
    @Override
    public Response<Long> isExist(Long breedFactoryId) {
        try {
            Map<String , Object> criteria = Maps.newHashMap();
            criteria.put("breedFactoryId",breedFactoryId);
            criteria.put("status",1);
            return Response.ok(breedFactoryStableDao.isExist(criteria));
        }catch (Exception e){
            log.error("failed to get stable case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to get stable");
        }
    }
}
